<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\common\model;


/**
 * Transfer模型
 */
class Transfer extends ModelBase
{

    protected $insert = ['create_time','serial_number'];
    protected $update = ['update_time'];

    /**
     * 流水号自动完成
     */
    public function setSerialNumberAttr($value)
    {

        return date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8).rand(1000,9999);
    }

    /**
     * 连用户表
     */
    public function member(){
        return $this->belongsTo('Member','user_id','id');
    }

    /**
     * 连商铺表
     */
    public function shop(){
        return $this->belongsTo('OtcShop','shop_id','id');
    }

    /**
     * 连钱包表
     */
    public function wallet(){
        return $this->belongsTo('Wallet','form_wallet_id','id');
    }

    /**
     * 连支付方式表
     */
    public function walletRechargeType(){
        return $this->belongsTo('UserPayType','paytype','id');
    }
    /**
     * 连支付方式表
     */
    public function withpay(){
        return $this->hasOne('WithdrawalPayinfo','withdrawal_id','id');
    }


    /**
     * 连游戏表
     */
    public function game(){
        return $this->belongsTo('Game','game_id','id');
    }

    /**
     * 连钱包表
     */
    public function towallet(){
        return $this->belongsTo('Wallet','to_wallet_id','id');
    }


    /**
     * 连游戏表
     */
    public function course(){
        return $this->belongsTo('Course','collection_id','id');
    }
    /**
     * 连游戏表
     */
    public function collection(){
        return $this->belongsTo('CourseCollections','collection_id','id');
    }


    /**
     * 获取用户钱包信息
     */
    public function getAccountAttr(){
        if(!empty($this->form_wallet_id)){
            return UserAccount::get(['user_id'=>$this->user_id,'wallet_id'=>$this->form_wallet_id]);
        }

    }
    /**
     * 获取收款方用户名信息
     */
    public function getUsernicknameAttr(){

        if($this->uid){
            return Member::where(['id'=>$this->uid])->value('username');
        }else{
            return '';
        }

    }


       /**
     * 获取状态信息
     */
    public function getStateTextAttr(){
        $arr=['待审核','成功','失败','打款中','等待匹配','商家已接单','商家已打款','商家打款超时','放行超时','申诉中'];
        return lang($arr[$this->state]);
    }

    public function getOtcOrderAttr(){
        return  OtcOrder::where(['withdrwal_id'=>$this->id])->order(['id desc'])->find();
    }


    /**
     * 获取状态信息
     */
    public function getExchangeTypeAttr($value){

        switch ($value){
            case 1:
                return  '总包转入理财包';
                break;
            case 2:
                return  '理财包转入总包';
                break;
        }


    }

    /**
     * 获取状态信息
     */
    public function getPayTextAttr(){
        $str='';
        if(!empty($this->pay_json)){

            $pay_json=json_decode($this->pay_json,true);

            foreach ($pay_json as $k=>$v){
                if($k==0){
                    switch ($v['type']){
                        case 1:
                            $str.='支付宝';
                            break;
                        case 2:
                            $str.='微信';
                            break;
                        case 3:
                            $str.='银行卡';
                            break;
                    }
                }else{
                    switch ($v['type']){
                        case 1:
                            $str.=',支付宝';
                            break;
                        case 2:
                            $str.=',微信';
                            break;
                        case 3:
                            $str.=',银行卡';
                            break;
                    }
                }
            }

        }else{
//

        }
        return $str;
    }

    /**
     * 获取是否支付宝
     */
    public function getIsAlipayAttr(){
        if(!empty($this->pay_json)){
            $pay_json=json_decode($this->pay_json,true);
            $state=0;
            foreach ($pay_json as $k=>$v){
                if($v['type']==1){
                   $state=1;
                    break;
                }
            }
            return $state;
        }
        return 0;


    }

    /**
     * 获取是否微信
     */
    public function getIsWeixinAttr(){
        if(!empty($this->pay_json)){
            $pay_json=json_decode($this->pay_json,true);
            $state=0;
            foreach ($pay_json as $k=>$v){
                if($v['type']==2){
                    $state=1;
                    break;
                }
            }
            return $state;
        }
        return 0;


    }

    /**
     * 获取是否银行卡
     */
    public function getIsBankAttr(){
        if(!empty($this->pay_json)){
            $pay_json=json_decode($this->pay_json,true);
            $state=0;
            foreach ($pay_json as $k=>$v){
                if($v['type']==3){
                    $state=1;
                    break;
                }
            }
            return $state;
        }
        return 0;


    }
    /**
     * 获取类型信息
     */
    public function getTypeTextAttr(){
        $arr=['','转让','转换','线下提现','线下充值','购买活动门票','矿工费转账','转入汇总地址','转入游戏','亏损补平','数字货币充值','区块链积分充值','人工充值','购买课程','打赏','游戏转入','币币账户到法币账户','法币账户到币币账户','OTC交易','币币置换','提币','充值游戏金豆','法币转入总包','店铺转入法币'];
        return lang($arr[$this->data['type']]);
    }
    /**
     * 获取余额信息
     */
    public function getBeforeAvailableBalanceAttr($value){
        if($this->type==4){
            if($this->state==1){
                return $value;
            }else{
                return UserAccount::where(['user_id'=>$this->user_id,'wallet_id'=>$this->form_wallet_id])->value('balance');
            }
        }
        return $value;

    }

    /**
     * 获取到账余额信息
     */
    public function getAccountBalanceAttr(){
        switch ($this->data['type']){
            case  4 :
                $proportion1 = $this->walletRechargeType->proportion1;
                return sprintf("%.2f",$proportion1 * $this->number);
                break;
            case  2 :
                $proportion1 = $this->wallet->conversion_available;
                return sprintf("%.2f",$proportion1 * $this->number);
                break;
            case  9 :
                $from_price = $this->wallet->parities;
                $to_price = $this->wallet()->where(['id'=>$this->to_wallet_id])->value('parities');
                return sprintf("%.8f",($this->number*$from_price/$to_price));
                break;
            default:
                return 0.00;
                break;
        }

    }

    /**
     * 获取余额信息
     */
    public function getAfterAvailableBalanceAttr($value){
        if($this->type==4){
            if($this->state==1){
                return $value;
            }else{
                $proportion1=$this->walletRechargeType->proportion1;
                return sprintf("%.2f",UserAccount::where(['user_id'=>$this->user_id,'wallet_id'=>$this->form_wallet_id])->value('balance')+ $this->number*$proportion1);
            }
        }
        return $value;

    }

    /**
     * logo
     */
    public function getNumberAttr($value)
    {
        if(in_array($this->form_wallet_id,[1,2,3])){
            return  sprintf("%.2f",$value);
        }else{
            return $value;
        }
    }

    /**
     * logo
     */
    public function getCcNumberAttr($value)
    {
        if(in_array($this->form_wallet_id,[1,2,3])){
            return  sprintf("%.2f",$value);
        }else{
            return $value;
        }
    }




    /**
     * logo
     */
    public function getPoundageNumberAttr($value)
    {
        if(in_array($this->form_wallet_id,[1,2,3])){
            return  sprintf("%.2f",$value);
        }else{
            return $value;
        }
    }

    /**
     * 连支付方式表
     */
    public function getUserpayAttr(){

        if($this->pay_json){
            $arr=json_decode($this->pay_json,true);
            return $arr;
        }
        return [];
    }
    /**
     * logo
     */
    public function getToNameAttr($value)
    {
        return  $this->wallet()->where(['id'=>$this->to_wallet_id])->value('name');
    }
    /**
     * 时间自动完成
     */
    public function setCreateTimeAttr()
    {

        return TIME_NOW;
    }
    /**
     * 时间自动完成
     */
    public function setUpdateTimeAttr()
    {

        return TIME_NOW;
    }

    /**
     * 时间自动完成
     */
    public function getCreateTimeAttr($value)
    {

        return date('Y-m-d H:i:s',$value);
    }
}
